﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Configuration;

namespace Publisher
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.txtSource.ReadOnly = true;
            this.txtDest.ReadOnly = true;
            this.txtSource.Text = ConfigurationManager.AppSettings["SourceDir"];
            this.txtDest.Text = ConfigurationManager.AppSettings["DestDir"];
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.btnSelectSource.Enabled = false;
            this.btnSelectDest.Enabled = false;
            this.btnStart.Enabled = false;
            string srcdir = this.txtSource.Text;
            string desdir = txtDest.Text;

            if (string.IsNullOrEmpty(srcdir))
            {
                MessageBox.Show("请选择源路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(desdir))
            {
                MessageBox.Show("请选择目标路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(srcdir))
            {
                MessageBox.Show("源路径不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!Directory.Exists(desdir))
            {
                MessageBox.Show("目标径不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CopyDirectory(srcdir, desdir);
            AccessAppSettings(srcdir, desdir);
            MessageBox.Show("操作完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.btnSelectSource.Enabled = true;
            this.btnSelectDest.Enabled = true;
            this.btnStart.Enabled = true;
        }

        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcdir"></param>
        /// <param name="desdir"></param>
        private void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
            string desfolderdir = desdir + "\\" + folderName;
            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {
                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (CheckDirectory(currentdir))
                    {
                        if (!Directory.Exists(currentdir))
                        {
                            Directory.CreateDirectory(currentdir);
                        }
                        CopyDirectory(file, desfolderdir);
                    }
                }
                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;

                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }

                    if (CheckFile(srcfileName))
                    {
                        File.Copy(file, srcfileName);
                    }
                }
            }//foreach 
        }

        private bool CheckFile(string srcfileName)
        {
            if (!srcfileName.EndsWith(".cs") && !srcfileName.EndsWith(".csproj.user") &&
                !srcfileName.EndsWith(".csproj") && !srcfileName.EndsWith(".sln"))
            {
                return true;
            }
            return false;
        }

        private bool CheckDirectory(string sdir)
        {
            if (!sdir.EndsWith(".BLL") && !sdir.EndsWith(".DAL"))
            {
                return true;
            }
            return false;
        }

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.txtSource.Text = fbd.SelectedPath;
            }
        }

        private void btnSelectDest_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.txtDest.Text = fbd.SelectedPath;
            }
        }


        private void AccessAppSettings(string srcdir, string desdir)
        {
            //获取Configuration对象
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            config.AppSettings.Settings.Remove("SourceDir");
            config.AppSettings.Settings.Remove("DestDir");

            config.AppSettings.Settings.Add("SourceDir", srcdir);
            config.AppSettings.Settings.Add("DestDir", desdir);

            //一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Full);
            //刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
